<?php
require_once dirname(__FILE__).'/config.php';
require_once cf\Config::path.'api/page.php';

class SiteMap
{
	function __construct($baseUrl)
	{
		$this->baseUrl = $baseUrl;
		$this->root = new SimpleXMLElement('<urlset/>');
		$this->root->addAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');
	}

	public function addUrl($url, $changefreq=null, $priority=null, $lastmod=null)
	{
		$el = $this->root->addChild('url');
		$el->addChild('loc',$this->baseUrl.$url);
		if ($lastmod) {
			$el->addChild('lastmod',$lastmod);
		}
		if ($changefreq) {
			$el->addChild('changefreq',$changefreq);
		}
		if ($priority) {
			$el->addChild('priority',$priority);
		}
	}
	
	public function addPages($pages=null, $changefreq=null, $priority=null, $lastmod=null) {
		$pages = is_array($pages) ? $pages : cf\api\page\getHierarchy();
		foreach ($pages as $page) {
			$this->addUrl($page['link'], $changefreq, $priority, $lastmod);
			$this->addPages($page['children'], $changefreq, $priority, $lastmod);
		}
	}

	function writeToFile($fpath,$fname='sitemap.xml')
	{
		file_put_contents($fpath.$fname,$this->root->asXML());
	}

	private $baseUrl = '';
	private $root = null;
};


?>